package com.cn.wanxi.util;

import com.cn.wanxi.dao.CompanyDao;

import java.io.*;
import java.util.List;

public class IO {
    private static File FILE = null;

    /**
     * 使用频率比较大static
     */
    private static void createNote(String fileName) {
        File directory = new File(PATH.PATH.getPath());
        if (!directory.exists()) {
            directory.mkdirs();
        }
        File file = new File(directory, fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FILE = file;
    }

    public static String readNote() {
        String result = "";
        try {
            Reader reader = new FileReader(FILE);
            long leng = 0;
            while ((leng = reader.read()) != -1) {
                result += (char) leng;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void wirteNote(String fileName, List<String> list) {
        createNote(fileName);
//        开始存入数据：这个地方，需要用到输入输出。需要明确什么是输入输出？
//       将dao的数据存入记事本   内存---记事本
        try {
            OutputStream outputStream = new FileOutputStream(FILE);
            for (int i = 0; i < list.size(); i++) {
                outputStream.write(list.get(i).getBytes());
                outputStream.write(PATH.SPLIT.getPath().getBytes());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
